/**
 * 
 * By Philip
 *
 *
 * Dieses Skript kann mehrere Waffen handeln, die mittels hitscan prüfen ob sie Gegner treffen
 * 


  Version History:
  
  V0.1: Basisfunktionen Implementiert
  V0.2: Ray wird von Waffenlauf gespawnt
  V0.3: Findet Gegnertyp Zombie und zieht im Leben ab
  V0.4: spricht Muzzle Flash Particle Emiter an
  V0.5: Gegner "Bluten" wenn sie getroffen werden, Partikelsystem in Gegner wird aktiviert
		Änderungen beim Ray: ray zeigt jetzt auf den Mauszeiger
  V1.0: Implementierung von ShootSpeed, Waffe kann jetzt nur noch so oft wie angegeben pro sekunde schießen



 **/



using UnityEngine;
using System.Collections;

public class WeaponController : MonoBehaviour {

	public int damage;

	public float shootSpeed; //Shootspeed interval in seconds e.g. 0.2f = 0.2 seconds cooldown time between shots
	private float coolDownTimer; //Tempoary coolDownTimer

	public Vector3 originPos;
	public Vector3 direction;

	public float maxDist; //maximum Weapon Reach

	public Renderer muzzleFlash; //muzzle Flash particle System
	private Ray muzzleFlashDir; //Direction of Muzzle Flash

	private Vector3 weaponHeight; // höhe der Waffe
	private Ray myRay; //Shoot Direction

	public ParticleSystem blood;





	// for debugging, green ray to show the shooting direction
	void OnDrawGizmos() {
		weaponHeight  = new Vector3 (0,1f,0);
		Gizmos.color = Color.red;
		Gizmos.DrawRay(GameObject.Find("Player").transform.position + weaponHeight,GetComponent<PlayerControllerScript>().lookRotationPoint - weaponHeight);

	}


	// Use this for initialization
	void Start () {
		weaponHeight  = new Vector3 (0,1f,0);
		originPos = GameObject.Find("Player").transform.position + weaponHeight; //ray spawns from the tip of the Gun
		direction = GetComponent<PlayerControllerScript>().lookRotationPoint - weaponHeight;
		maxDist = 100;
		shootSpeed = 0.4f;
		muzzleFlash.transform.position = GameObject.Find("RightGunTip").transform.position;
		muzzleFlash.particleEmitter.emit = false;
		coolDownTimer = shootSpeed;
		
	}
	
	// Update is called once per frame
	void Update () {
		originPos = GameObject.Find("RightGunTip").transform.position;
		direction = GameObject.FindGameObjectWithTag("Player").transform.forward;
		myRay = new Ray(originPos,direction);
		//muzzleFlash.transform.LookAt(muzzleFlashDir.GetPoint(100f),Vector3.up);

		RaycastHit hit;


		coolDownTimer -= Time.deltaTime;

		if(Input.GetMouseButtonDown(0) && coolDownTimer <= 0) //if you press the mouse button and weapon is not on cooldown
		{
			if(muzzleFlash)
			{
				muzzleFlash.particleEmitter.Emit(); //emit Shoot particles
			}
			if(Physics.Raycast(myRay, out hit,maxDist))
			{
			//Vector3 treffpunkt = hit.point;
				Collider enemy = hit.collider;
				if(enemy.tag == "Enemy")
				{
					enemy.GetComponent<ZombieAI>().health -= damage; //count down Health


					//Enable The Blood particle System
					enemy.GetComponentInChildren<ParticleSystem>().transform.LookAt(GameObject.FindGameObjectWithTag("Player").transform.position,Vector3.up);
					enemy.GetComponentInChildren<ParticleSystem>().enableEmission = true;
					enemy.GetComponentInChildren<ParticleSystem>().Play();
					//enemy.GetComponentInChildren<ParticleSystem>().enableEmission = false;
					//enemy.GetComponentInChildren<ParticleSystem>().isPlaying = true;
				}
			}
			coolDownTimer = shootSpeed;
		}

	}


}
